package com.valley.dream.tihai;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.valley.dream.tihai.model.dto.ProblemParcelable;
import com.valley.dream.tihai.view.activity.CommunityActivity;
import com.valley.dream.tihai.view.fragment.CommunityFragment;
import com.valley.dream.tihai.view.fragment.DataFragment;
import com.valley.dream.tihai.view.fragment.MeFragment;
import com.valley.dream.tihai.view.fragment.RecommendFragment;
import com.valley.dream.tihai.view.activity.SubmitProblemActivity;
import com.valley.dream.tihai.view.adapter.InitFragmentAdapter;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager2 viewPager2;
    private ImageView recommendImage, communityImage, dataImage, meImage, currentImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化View界面
        initView();
    }

    private void initView() {
        //初始化ViewPager2
        initViewPager2();
        //初始化底部按钮
        initImageView();
    }

    //初始化ViewPager2
    private void initViewPager2() {
        viewPager2 = findViewById(R.id.main_fragment_layout);
        InitFragmentAdapter adapter = new InitFragmentAdapter(getSupportFragmentManager(), getLifecycle());
        //初始化主页Fragment
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new RecommendFragment());
        fragmentList.add(new CommunityFragment());
        fragmentList.add(new DataFragment());
        fragmentList.add(new MeFragment());
        adapter.setFragmentList(fragmentList);
        viewPager2.setAdapter(adapter);

        //设置viewPager监听事件
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                changeViewPager(position);
            }
        });
    }

    //改变ViePager页面
    private void changeViewPager(int position) {
        currentImage.setSelected(false);
        switch (position){
            case 0:
                recommendImage.setSelected(true);
                currentImage = recommendImage;
                break;
            case 1:
                communityImage.setSelected(true);
                currentImage = communityImage;
                break;
            case 2:
                dataImage.setSelected(true);
                currentImage = dataImage;
                break;
            case 3:
                meImage.setSelected(true);
                currentImage = meImage;
                break;
        }
    }

    //初始化底部按钮
    private void initImageView() {
        recommendImage = findViewById(R.id.main_image_recommend);
        communityImage = findViewById(R.id.main_image_community);
        dataImage = findViewById(R.id.main_image_data);
        meImage = findViewById(R.id.main_image_me);

        currentImage = recommendImage;
        currentImage.setSelected(true);
    }

    //改变底部导航栏图标
    public void changeMainImage(View view) {
        currentImage.setSelected(false);
        switch (view.getId()){
            case R.id.main_image_recommend:
                viewPager2.setCurrentItem(0);
                recommendImage.setSelected(true);
                currentImage = recommendImage;
                break;
            case R.id.main_image_community:
                viewPager2.setCurrentItem(1);
                communityImage.setSelected(true);
                currentImage = communityImage;
                break;
            case R.id.main_image_data:
                viewPager2.setCurrentItem(2);
                dataImage.setSelected(true);
                currentImage = dataImage;
                break;
            case R.id.main_image_me:
                viewPager2.setCurrentItem(3);
                meImage.setSelected(true);
                currentImage = meImage;
                break;
        }
    }

    public void submitProblemActivity(View view) {
        ProblemParcelable problemParcelable = new ProblemParcelable();
        problemParcelable.setTypeId(1);
        problemParcelable.setUserId(1);
        Intent intent = new Intent(this, SubmitProblemActivity.class);
        intent.putExtra("problem", problemParcelable);
        startActivity(intent);
        finish();
    }

    public void enterCommunity(int id) {
        Intent intent = new Intent(this, CommunityActivity.class);
        intent.putExtra("id",id);
        startActivity(intent);
        finish();
    }
}